<?php

class Auditoria {

    private
            $sql,
            $conn;
    public
            $total,
            $search,
            $min,
            $max;
    public
            $p_aud_id,
            $p_aud_id_usuario,
            $p_aud_fecha,
            $p_aud_operacion,
            $p_aud_modulo,
            $p_aud_registro_anterior,
            $p_aud_registro_actual,
            $p_aud_ip,
            $p_search,
            $p_subconsulta,
            $p_groupby,
            $p_orderby,
            $p_min,
            $p_max,
            $p_opc;

    public function __construct($conn, $min = "", $max = "") {
        $this->conn = $conn;
        $this->p_min = $min;
        $this->p_max = $max;
    }

    function clean() {
        $this->p_aud_id = "";
        $this->p_aud_id_usuario = "";
        $this->p_aud_fecha = "";
        $this->p_aud_operacion = "";
        $this->p_aud_modulo = "";
        $this->p_aud_registro_anterior = "";
        $this->p_aud_registro_actual = "";
        $this->p_aud_ip = "";
        $this->p_search = "";
        $this->p_subconsulta = "";
        $this->p_groupby = "";
        $this->p_orderby = "";
        $this->p_min = "";
        $this->p_max = "";
        $this->p_opc = "";
    }

    private function sql_query($operacion) {
        $this->str_sql = $this->sql = "CALL pro_auditoria_sistema(" .
                "'$this->p_aud_id'," .
                "'$this->p_aud_id_usuario'," .
                "'$this->p_aud_fecha'," .
                "'$this->p_aud_operacion'," .
                "'$this->p_aud_modulo'," .
                "'$this->p_aud_registro_anterior'," .
                "'$this->p_aud_registro_actual'," .
                "'$this->p_aud_ip'," .
                "'$this->p_search'," .
                "'$this->p_subconsulta'," .
                "'$this->p_groupby'," .
                "'$this->p_orderby'," .
                "'$this->p_min'," .
                "'$this->p_max'," .
                "'$operacion'" .
                ")";


        $this->sql = mysql_query($this->sql, $this->conn);

        /** Se extrae la ID de la ultima inserci�n... */
        if ($operacion == 'registrar' and $this->sql) {
            $last_id = mysql_fetch_array($this->sql);
            return $last_id[0];
        }
    }

    function insert() {
        $last_id = $this->sql_query('registrar');
        mysql_close($this->conn);
        return $last_id;
    }

    function update($id) {
        $this->p_usu_id = $id;
        $this->sql_query('modificar');
        mysql_close($this->conn);
        return $this->sql;
    }

    function delete($id) {
        $this->p_usu_id = $id;
        $this->sql_query('eliminar');
        mysql_close($this->conn);
        return $this->sql;
    }

    /* */

    function consult($id = "") {
        $this->p_usu_id = $id;
        $this->sql_query('consultar');
        $this->clean();
        $i = 0;
        while (
        list(
        $this->p_aud_id [$i],
        $this->p_aud_id_usuario [$i],
        $this->p_aud_fecha [$i],
        $this->p_aud_operacion [$i],
        $this->p_aud_modulo [$i],
        $this->p_aud_registro_anterior[$i],
        $this->p_aud_registro_actual [$i],
        $this->p_aud_ip [$i]

        ) = mysql_fetch_array($this->sql)
        ) {
            $i++;
        }
        $this->total = $i;
        mysql_close($this->conn);
        return $this->sql;
    }

}

?>
